<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Linux命令," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;通过SSH访问服务器，难免会要用到压缩，解压缩，打包，解包等，这时候tar命令就是是必不可少的一个功能强大的工具。linux中最流行的tar是麻雀虽小，五脏俱全，功能强大。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;tar命令可以为linux的文件和">
<meta name="keywords" content="Linux命令">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux 命令- tar">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/2. Linux 命令/28. Linux 命令- tar/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;通过SSH访问服务器，难免会要用到压缩，解压缩，打包，解包等，这时候tar命令就是是必不可少的一个功能强大的工具。linux中最流行的tar是麻雀虽小，五脏俱全，功能强大。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;tar命令可以为linux的文件和">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:34:47.229Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux 命令- tar">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;通过SSH访问服务器，难免会要用到压缩，解压缩，打包，解包等，这时候tar命令就是是必不可少的一个功能强大的工具。linux中最流行的tar是麻雀虽小，五脏俱全，功能强大。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;tar命令可以为linux的文件和">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/2. Linux 命令/28. Linux 命令- tar/"/>





  <title>Linux 命令- tar | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/2. Linux 命令/28. Linux 命令- tar/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Linux 命令- tar</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T01:57:34+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过SSH访问服务器，难免会要用到压缩，解压缩，打包，解包等，这时候tar命令就是是必不可少的一个功能强大的工具。linux中最流行的tar是麻雀虽小，五脏俱全，功能强大。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tar命令可以为linux的文件和目录创建档案。利用tar，可以为某一特定文件创建档案（备份文件），也可以在档案中改变文件，或者向档案中加入新的文件。tar最初被用来在磁带上创建档案，现在，用户可以在任何设备上创建档案。利用tar命令，可以把一大堆的文件和目录全部打包成一个文件，这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要弄清两个概念：打包和压缩。打包是指将一大堆文件或目录变成一个总的文件；压缩则是将一个大的文件通过一些压缩算法变成一个小文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为什么要区分这两个概念呢？这源于Linux中很多压缩程序只能针对一个文件进行压缩，这样当你想要压缩一大堆文件时，你得先将这一大堆文件先打成一个包（tar命令），然后再用压缩程序进行压缩（gzip bzip2命令）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux下最常用的打包程序就是tar了，使用tar程序打出来的包我们常称为tar包，tar包文件的命令通常都是以.tar结尾的。生成tar包后，就可以用其它的程序来进行压缩。</p>
<h2 id="1-命令格式"><a href="#1-命令格式" class="headerlink" title="1.命令格式"></a>1.命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar [必要参数] [选择参数] [文件]</div></pre></td></tr></table></figure>
<h2 id="2-命令功能"><a href="#2-命令功能" class="headerlink" title="2.命令功能"></a>2.命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用来压缩和解压文件。tar 本身不具有压缩功能。它是调用压缩功能实现的。</p>
<h2 id="3-命令参数"><a href="#3-命令参数" class="headerlink" title="3.命令参数"></a>3.命令参数</h2><h3 id="必要参数"><a href="#必要参数" class="headerlink" title="必要参数"></a>必要参数</h3><ul>
<li>-A 新增压缩文件到已存在的压缩</li>
<li>-B 设置区块大小</li>
<li>-c 建立新的压缩文件</li>
<li>-d 记录文件的差别</li>
<li>-r 添加文件到已经压缩的文件</li>
<li>-u 添加改变了和现有的文件到已经存在的压缩文件</li>
<li>-x 从压缩的文件中提取文件</li>
<li>-t 显示压缩文件的内容</li>
<li>-z 支持gzip解压文件</li>
<li>-j 支持bzip2解压文件</li>
<li>-Z 支持compress解压文件</li>
<li>-v 显示操作过程</li>
<li>-l 文件系统边界设置</li>
<li>-k 保留原有文件不覆盖</li>
<li>-m 保留文件不被覆盖</li>
<li>-W 确认压缩文件的正确性</li>
</ul>
<h3 id="可选择参数"><a href="#可选择参数" class="headerlink" title="可选择参数"></a>可选择参数</h3><ul>
<li>-b 设置区块数目</li>
<li>-C 切换到指定目录</li>
<li>-f 指定压缩文件</li>
<li>–help 显示帮助信息</li>
<li>–version 显示版本信息</li>
</ul>
<h2 id="4-常见压缩、解压命令"><a href="#4-常见压缩、解压命令" class="headerlink" title="4.常见压缩、解压命令"></a>4.常见压缩、解压命令</h2><h3 id="tar"><a href="#tar" class="headerlink" title="tar"></a>tar</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar xvf filname.tar</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;打包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar cvf filename.tar dirname</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：tar 是打包，不是压缩！</p>
<h3 id="gz"><a href="#gz" class="headerlink" title=".gz"></a>.gz</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gunzip filename.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压2</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip -d filename.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">gzip filename.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar zxvf filename.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar zcvf filename.tar.gz dirname</div></pre></td></tr></table></figure>
<h3 id="bz2"><a href="#bz2" class="headerlink" title=".bz2"></a>.bz2</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压1</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bzip2 -d filename.bz2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压2</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bunzip2 filename.bz2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bzip2 -z filename.tar.bz2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar jxvf filename.tar.bz2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar jcvf filename.tar.bz2 diename</div></pre></td></tr></table></figure>
<h3 id="Z"><a href="#Z" class="headerlink" title=".Z"></a>.Z</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">unconpress filename.Z</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">compress filename.tar.Z</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar Zxvf filename.tar.Z</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar Zcvf filename.tar.Z dirname</div></pre></td></tr></table></figure>
<h3 id="zip"><a href="#zip" class="headerlink" title=".zip"></a>.zip</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">unzip filename.zip</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">zip filename.zip dirname</div></pre></td></tr></table></figure>
<h3 id="rar"><a href="#rar" class="headerlink" title=".rar"></a>.rar</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rar x filename.rar</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rar a filename.rar dirname</div></pre></td></tr></table></figure>
<h2 id="5-使用实例"><a href="#5-使用实例" class="headerlink" title="5.使用实例"></a>5.使用实例</h2><h3 id="实例1：将文件全部打包成-tar-包"><a href="#实例1：将文件全部打包成-tar-包" class="headerlink" title="实例1：将文件全部打包成 tar 包"></a>实例1：将文件全部打包成 tar 包</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">tar -cvf log.tar log2012.log</div><div class="line">tar -zxvf log.tar.gz log2012.log</div><div class="line">tar -jcvf log.tar.bz2 log2012.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -al log2012.log</span></div><div class="line">---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -cvf log.tar log2012.log </span></div><div class="line">log2012.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -zcvf log.tar.gz log2012.log</span></div><div class="line">log2012.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -jcvf log.tar.bz2 log2012.log </span></div><div class="line">log2012.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -al *.tar*</span></div><div class="line">-rw-r--r-- 1 root root 307200 11-29 17:54 log.tar</div><div class="line">-rw-r--r-- 1 root root   1413 11-29 17:55 log.tar.bz2</div><div class="line">-rw-r--r-- 1 root root   1413 11-29 17:54 log.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<ol>
<li>tar -cvf log.tar log2012.log    仅打包，不压缩！     </li>
<li>tar -zcvf log.tar.gz log2012.log   打包后，以 gzip 压缩     </li>
<li>tar -zcvf log.tar.bz2 log2012.log  打包后，以 bzip2 压缩     </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在参数 f 之后的文件档名是自己去的，习惯上都用 .tar 来作为辨识。如果加 z 参数，则以 .tar.gz 或 .tgz 来代表 gzip 压缩过的 tar 包；如果加 j 参数，则以 .tar.bz2 来作为 tar 报名。</p>
<h3 id="实例2：查阅上述-tar-包内有哪些文件"><a href="#实例2：查阅上述-tar-包内有哪些文件" class="headerlink" title="实例2：查阅上述 tar 包内有哪些文件"></a>实例2：查阅上述 tar 包内有哪些文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar -ztvf log.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -ztvf log.tar.gz</span></div><div class="line">---xrw-r-- root/root    302108 2012-11-13 06:03:25 log2012.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于使用 gzip 压缩的 log.tar.gz，所以要查阅 log.tar.gz 包内的文件时，就要加上 z 这个参数</p>
<h3 id="实例3：将-tar-包解压缩"><a href="#实例3：将-tar-包解压缩" class="headerlink" title="实例3：将 tar 包解压缩"></a>实例3：将 tar 包解压缩</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar -zxvf /opt/soft/<span class="built_in">test</span>/log.tar.gz</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 0[root@localhost test3]<span class="comment"># tar -zxvf /opt/soft/test/log.tar.gz</span></div><div class="line">log2012.log</div><div class="line">[root@localhost test3]<span class="comment"># ls</span></div><div class="line">log2012.log</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在预设的情况下，可以将压缩当在任何地方解压</p>
<h3 id="实例4：只将-tar-内的部分文件解压出来"><a href="#实例4：只将-tar-内的部分文件解压出来" class="headerlink" title="实例4：只将 /tar 内的部分文件解压出来"></a>实例4：只将 /tar 内的部分文件解压出来</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar -zxvf /opt/soft/<span class="built_in">test</span>/log30.tar.gz log2013.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -zcvf log30.tar.gz log2012.log log2013.log </span></div><div class="line">log2012.log</div><div class="line">log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ls -al log30.tar.gz </span></div><div class="line">-rw-r--r-- 1 root root 1512 11-30 08:19 log30.tar.gz</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -zxvf log30.tar.gz log2013.log</span></div><div class="line">log2013.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root   1512 11-30 08:19 log30.tar.gz</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># tar -zxvf /opt/soft/test/log30.tar.gz log2013.log</span></div><div class="line">log2013.log</div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 4</div><div class="line">-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以投过 tar -ztvf 来查阅 tar 包内的文件名称，如果单只要一个文件，就可透过这个方式来解压部分文件！</p>
<h2 id="实例5：文件备份下来，并且保存其权限"><a href="#实例5：文件备份下来，并且保存其权限" class="headerlink" title="实例5：文件备份下来，并且保存其权限"></a>实例5：文件备份下来，并且保存其权限</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar -zcvpf log31.tar.gz log2014.log log2015.log lo2016.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log</div><div class="line">-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log</div><div class="line">-rw-r--r-- 1 root root      0 11-16 14:41 log2016.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log </span></div><div class="line">log2014.log</div><div class="line">log2015.log</div><div class="line">log2016.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test6</span></div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">[root@localhost test6]<span class="comment"># tar -zxvpf /opt/soft/test/log31.tar.gz </span></div><div class="line">log2014.log</div><div class="line">log2015.log</div><div class="line">log2016.log</div><div class="line">[root@localhost test6]<span class="comment"># ll</span></div><div class="line">总计 0</div><div class="line">-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log</div><div class="line">-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log</div><div class="line">-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log</div><div class="line">[root@localhost test6]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个 -p 的属性是很重要的，尤其是要保留原文本文件的属性时</p>
<h3 id="实例6：在文件夹当中，比某个日期新的文件才备份"><a href="#实例6：在文件夹当中，比某个日期新的文件才备份" class="headerlink" title="实例6：在文件夹当中，比某个日期新的文件才备份"></a>实例6：在文件夹当中，比某个日期新的文件才备份</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar -N <span class="string">"2012/11/13"</span> -zcvf log17.tar.gz <span class="built_in">test</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[root@localhost soft]<span class="comment"># tar -N "2012/11/13" -zcvf log17.tar.gz test</span></div><div class="line">tar: Treating date `2012/11/13<span class="string">' as 2012-11-13 00:00:00 + 0 nanoseconds</span></div><div class="line"><span class="string">test/test/log31.tar.gz</span></div><div class="line"><span class="string">test/log2014.log</span></div><div class="line"><span class="string">test/linklog.log</span></div><div class="line"><span class="string">test/log2015.log</span></div><div class="line"><span class="string">test/log2013.log</span></div><div class="line"><span class="string">test/log2012.log</span></div><div class="line"><span class="string">test/log2017.log</span></div><div class="line"><span class="string">test/log2016.log</span></div><div class="line"><span class="string">test/log30.tar.gz</span></div><div class="line"><span class="string">test/log.tar</span></div><div class="line"><span class="string">test/log.tar.bz2</span></div><div class="line"><span class="string">test/log.tar.gz</span></div></pre></td></tr></table></figure>
<h3 id="实例7：备份文件夹内容是排除部分文件"><a href="#实例7：备份文件夹内容是排除部分文件" class="headerlink" title="实例7：备份文件夹内容是排除部分文件"></a>实例7：备份文件夹内容是排除部分文件</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">tar --exclide scf/service -zcvf scf.tar/gz scf/*</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tree scf</span></div><div class="line">scf</div><div class="line">|-- bin</div><div class="line">|-- doc</div><div class="line">|-- lib</div><div class="line">`-- service</div><div class="line">   	 `-- deploy</div><div class="line">       	 	|-- info</div><div class="line">       	 	`-- product</div><div class="line">7 directories, 0 files</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># tar --exclude scf/service -zcvf scf.tar.gz scf/* </span></div><div class="line">scf/bin/</div><div class="line">scf/doc/</div><div class="line">scf/lib/</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Linux命令/" rel="tag"># Linux命令</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/2. Linux 命令/36. Linux 命令- du/" rel="next" title="Linux 命令- du">
                <i class="fa fa-chevron-left"></i> Linux 命令- du
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/2. Linux 命令/6. Linux 命令- rm/" rel="prev" title="Linux 命令- rm">
                Linux 命令- rm <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-命令格式"><span class="nav-number">1.</span> <span class="nav-text">1.命令格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-命令功能"><span class="nav-number">2.</span> <span class="nav-text">2.命令功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-命令参数"><span class="nav-number">3.</span> <span class="nav-text">3.命令参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#必要参数"><span class="nav-number">3.1.</span> <span class="nav-text">必要参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#可选择参数"><span class="nav-number">3.2.</span> <span class="nav-text">可选择参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-常见压缩、解压命令"><span class="nav-number">4.</span> <span class="nav-text">4.常见压缩、解压命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#tar"><span class="nav-number">4.1.</span> <span class="nav-text">tar</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gz"><span class="nav-number">4.2.</span> <span class="nav-text">.gz</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bz2"><span class="nav-number">4.3.</span> <span class="nav-text">.bz2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Z"><span class="nav-number">4.4.</span> <span class="nav-text">.Z</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#zip"><span class="nav-number">4.5.</span> <span class="nav-text">.zip</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rar"><span class="nav-number">4.6.</span> <span class="nav-text">.rar</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-使用实例"><span class="nav-number">5.</span> <span class="nav-text">5.使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实例1：将文件全部打包成-tar-包"><span class="nav-number">5.1.</span> <span class="nav-text">实例1：将文件全部打包成 tar 包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例2：查阅上述-tar-包内有哪些文件"><span class="nav-number">5.2.</span> <span class="nav-text">实例2：查阅上述 tar 包内有哪些文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例3：将-tar-包解压缩"><span class="nav-number">5.3.</span> <span class="nav-text">实例3：将 tar 包解压缩</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例4：只将-tar-内的部分文件解压出来"><span class="nav-number">5.4.</span> <span class="nav-text">实例4：只将 /tar 内的部分文件解压出来</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例5：文件备份下来，并且保存其权限"><span class="nav-number">6.</span> <span class="nav-text">实例5：文件备份下来，并且保存其权限</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#实例6：在文件夹当中，比某个日期新的文件才备份"><span class="nav-number">6.1.</span> <span class="nav-text">实例6：在文件夹当中，比某个日期新的文件才备份</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例7：备份文件夹内容是排除部分文件"><span class="nav-number">6.2.</span> <span class="nav-text">实例7：备份文件夹内容是排除部分文件</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
